package com.ruoyi.system.service.impl;

import java.util.List;

import com.aliyuncs.exceptions.ClientException;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.aliyun.SendMessage;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.MsgConfiguration;
import com.ruoyi.system.mapper.MsgConfigurationMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MsgLogMapper;
import com.ruoyi.system.domain.MsgLog;
import com.ruoyi.system.service.IMsgLogService;

/**
 * 消息日志Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-01
 */
@Service
public class MsgLogServiceImpl implements IMsgLogService
{
    @Autowired
    private MsgLogMapper msgLogMapper;
    @Autowired
    private MsgConfigurationMapper msgConfigurationMapper;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询消息日志
     *
     * @param id 消息日志主键
     * @return 消息日志
     */
    @Override
    public MsgLog selectMsgLogById(String id)
    {
        return msgLogMapper.selectMsgLogById(id);
    }

    /**
     * 查询消息日志列表
     *
     * @param msgLog 消息日志
     * @return 消息日志
     */
    @Override
    public List<MsgLog> selectMsgLogList(MsgLog msgLog)
    {
        return msgLogMapper.selectMsgLogList(msgLog);
    }

    /**
     * 新增消息日志
     *
     * @param msgLog 消息日志
     * @return 结果
     */
    @Override
    public int insertMsgLog(MsgLog msgLog)
    {
        msgLog.setCreateTime(DateUtils.getNowDate());
        String date = DateUtils.dateTimeNow();
        String id = date + IdUtils.fastSimpleEnglishUUID(5);
        msgLog.setId(id);
        return msgLogMapper.insertMsgLog(msgLog);
    }

    /**
     * 修改消息日志
     *
     * @param msgLog 消息日志
     * @return 结果
     */
    @Override
    public int updateMsgLog(MsgLog msgLog)
    {
        MsgLog msgLogVo = new MsgLog();
        msgLogVo.setUpdateTime(DateUtils.getNowDate());
        msgLogVo.setId(msgLog.getId());
        msgLogVo.setStatus(msgLog.getStatus());
        return msgLogMapper.updateMsgLog(msgLog);
    }

    /**
     * 删除消息日志信息
     *
     * @param id 消息日志主键
     * @return 结果
     */
    @Override
    public int deleteMsgLogById(String id)
    {
        return msgLogMapper.deleteMsgLogById(id);
    }

    /**
     * 发送消息
     *
     * @return 结果
     */
    @Override
    public void sendMsg(Long msgConfigurationId,String userId){
        MsgConfiguration msgConfiguration = msgConfigurationMapper.selectMsgConfigurationById(msgConfigurationId);
        String sendType = msgConfiguration.getSendType();
        //消息内容
        MsgLog msgLog = new MsgLog();
        msgLog.setMsgContent(msgConfiguration.getMsgContent());
        msgLog.setMsgTitile(msgConfiguration.getMsgTitile());
        msgLog.setReceiver(userId);
        SysUser user = userService.selectUserById(Long.valueOf(userId));
        String phonenumber = user.getPhonenumber();

        if("0".equals(sendType)){
            //站内消息
            msgLog.setSendType("0");
            insertMsgLog(msgLog);
        }else if("1".equals(sendType)){
            //短信
            sendTextMsg(userId,phonenumber,msgConfiguration);
        }else if("2".equals(sendType)){
            //短信
            sendTextMsg(userId,phonenumber,msgConfiguration);
            //站内消息
            msgLog.setSendType("0");
            insertMsgLog(msgLog);
        }
    }

    //发送短信提醒
    public void sendTextMsg(String userId, String phonenumber,MsgConfiguration msgConfiguration){
        Integer msgCon = 1111;
        try {
            MsgLog msgLog = new MsgLog();
            msgLog.setMsgContent(msgConfiguration.getMsgContent());
            msgLog.setMsgTitile(msgConfiguration.getMsgTitile());
            msgLog.setReceiver(userId);
            //站内消息
            msgLog.setSendType("1");
            //消息发送失败
            if (!SendMessage.sendSms(phonenumber, msgCon)) {
                msgLog.setStatus("1");
                insertMsgLog(msgLog);
            }
            insertMsgLog(msgLog);
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
